package pers.lt.admin.dao;

import pers.lt.admin.dao.interfaces.UserRightsDao;
import pers.lt.tools.StringFacs;
import pers.lt.jdbc.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liting on 2016/7/27.
 * 权限管理，查询用户拥有的角色 ，查询权限对应的角色
 */
@Component("userRightsDao")
public class UserRightsDaoImp implements UserRightsDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List userRoleInfoList(String userId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select role_id from role_user_info where 1 = 1 ");
        if(!StringFacs.isEmpty(userId)) sql.append(" and user_id='").append(userId).append("'");
        return jdbcTemplate.queryDatas(sql.toString());
    }

    @Override
    public List rightsRoleInfoList() {
        List list = new ArrayList();
        //查出权限对应的角色
        List<Map> roleRightsList = jdbcTemplate.queryDatas("select * from role_rights_info t");
        //查出所有权限
        List<Map> rightList = jdbcTemplate.queryDatas("select * from rights_info t");
        //遍历所有权限
        for(Map map:rightList){
            //组织成Map权限
            Map rightsRoleMap  = new HashMap();
            rightsRoleMap.put("righturl",map.get("RIGHT_URL").toString());
            //装角色的LIST
            List<String> roleList = new ArrayList<String>();
            //遍历权限对应角色关系
            for(Map map1:roleRightsList){
                //要据权限ID，组织一个权限对应的所有角色集合
                if(map.get("ID").equals(map1.get("RIGHT_ID"))) roleList.add(map1.get("ROLE_ID").toString());
            }
            rightsRoleMap.put("rightroles",roleList);
            list.add(rightsRoleMap);
        }
        return list;
    }
}
